package com.hcms.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hcms.mybatis.entity.SysRolePermission;
import com.hcms.admin.service.ISysRolePermissionService;
import com.hcms.mybatis.mapper.SysRolePermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 后台用户角色和权限关系表 服务实现类
 * </p>
 *
 * @author achenxw@126.com
 * @since 2023-02-13
 */
@Service
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements ISysRolePermissionService {

    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Override
    public List<String> getPermissionsByRoleId(Long roleId) {
        LambdaQueryWrapper<SysRolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRolePermission::getRoleId, roleId);
        List<SysRolePermission> relations = sysRolePermissionMapper.selectList(queryWrapper);
        return relations.stream().map(SysRolePermission::getPermission).collect(Collectors.toList());
    }

    @Override
    public List<String> getPermissionsByRoleId(Long[] roleIds) {
        LambdaQueryWrapper<SysRolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysRolePermission::getRoleId, roleIds);
        List<SysRolePermission> relations = sysRolePermissionMapper.selectList(queryWrapper);
        return relations.stream().map(SysRolePermission::getPermission).collect(Collectors.toList());
    }
}
